<?php

namespace app\index\controller;
use think\Db;
use \think\Cookie;
use \think\Model;
use think\Session;
class User extends Model
{
    protected $createTime = 'regtime';
    protected $updateTime = 'lasttime';
}
class Forget extends \think\Controller
{
    public function forget()    //找回密码
    {

        if (input('post.mail')) {
            $mail = input('post.mail');
            $qq = input('post.qq');
            $forget_user = Db::name('user')->where(['mail' => $mail, 'qq' => $qq])->find();
            if (empty($forget_user)) {
                $this->error('QQ或者邮箱填写错误');
            } else {
                $url = \request()->domain();
                $toemail = $mail;
                $name = $forget_user['name'];
                $password = $forget_user['password'];
                $subject = '忆流年小站-找回密码';
                $content = '请点击以下链接继续接下来找回密码的操作哦：' . $url . '/index/Forget/forget.html?pwd=' . $password . '&safecode=' . $forget_user['safecode'];
                if (send_mail($toemail, $name, $subject, $content)) {
                    $this->success('邮件发送成功，请在邮箱中完成最后的操作哦', '../../index/Login/login');
                }
            }
//
        }
        if (input('get.pwd') && input('get.safecode')) {
            $pwd = input('get.pwd');
            $safecode = input('get.safecode');
            if (input('post.new_pwd')) {
                if (Db::name('user')->where(['password' => $pwd, 'safecode' => $safecode])->update(['password' => md5(input('post.new_pwd'))])) {
                    $this->success('密码修改成功，请登录吧', '../../index/Login/login');
                } else {
                    $this->error('修改失败');
                }

            } else {
                $webdate = Db::name('index')->getById(1);//根据用户识别码
                //渲染模板
                $this->assign('name', $webdate['name']);
                $this->assign('copy', $webdate['copy']);
                $this->assign('pwd', $pwd);
                $this->assign('safecode', $safecode);
                return $this->fetch('index/update_password2', [], ['__PUBLIC__' => '/public/static/home']);
            }

        } else {
            $webdate = Db::name('index')->getById(1);//根据用户识别码
            //渲染模板
            $this->assign('name', $webdate['name']);
            $this->assign('copy', $webdate['copy']);
            return $this->fetch('index/forget1', [], ['__PUBLIC__' => '/public/static/home']);
        }

    }
}
